package com.yanqu.road.dataHelper.action.check;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 停服跑一下，生活更健康
 * @Author xx
 * @Date 2020/11/11
 */
public class CheckUserData extends BaseRepair {

    public CheckUserData(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void go(){
        List<Map<String,String>> listMap = checkVehiclePatrons();
        DataMgr.makeTextData("珍兽含有的门客,却没有对应门客"+getGameServer().getServerInfo().getServerId(),listMap);
        listMap = checkBuildingPatrons();
        DataMgr.makeTextData("建筑含有的门客,却没有对应门客"+getGameServer().getServerInfo().getServerId(),listMap);
        listMap = checkBanquetBeauties();
        DataMgr.makeTextData("婚贴有这个美女,却没有对应美女"+getGameServer().getServerInfo().getServerId(),listMap);
        listMap = checkBanquetBeauties();
        DataMgr.makeTextData("婚宴有这个美女,却没有对应美女"+getGameServer().getServerInfo().getServerId(),listMap);
    }


    //座驾有这个门客，门客却没了
    private List<Map<String, String>> checkVehiclePatrons() {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT v.userId,v.patronsId,v.templateId FROM t_u_vehicle v" +
                " LEFT JOIN t_u_patrons p on v.userId = p.userId AND v.patronsId = p.patronsId" +
                " WHERE v.patronsId != 0 AND p.patronsId is null";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("patronsId",checkNullToEmpty(rs.getString("patronsId")));
                temp.put("templateId",checkNullToEmpty(rs.getString("templateId")));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }    //座驾有这个门客，门客却没了

    //建筑有这个门客，门客却没了
    private List<Map<String,String>> checkBuildingPatrons(){
        List<Map<String,String>> dataList = getBuildPatrons();
        List<Map<String,String>> noDataList = getNoPatrons(dataList);
        return noDataList;
    }
    private List<Map<String, String>> getBuildPatrons() {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT userId,buildingId,patronsIdList FROM t_u_building WHERE patronsIdList != ''";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                String patronsIdListStr = rs.getString("patronsIdList");
                if(!StringUtils.isNullOrEmpty(patronsIdListStr)){
                    List<Integer> patronsIdList = StringUtils.stringToIntegerList(patronsIdListStr,",");
                    for(Integer patronsId : patronsIdList){
                        Map<String,String> temp = new ConcurrentHashMap<>();
                        temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                        temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                        temp.put("buildingId",checkNullToEmpty(rs.getString("buildingId")));
                        temp.put("patronsId",String.valueOf(patronsId));
                        dataList.add(temp);
                    }
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getNoPatrons(List<Map<String,String>> data) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT userId,patronsId FROM t_u_patrons WHERE userId = ? AND patronsId = ?";
        for(Map<String,String> one : data){
            try {
                call = getGameServer().getConnection().prepareCall(sql);
                call.setString(1, one.get("userId"));
                call.setString(2, one.get("patronsId"));
                rs = call.executeQuery();
                if(!rs.next()) {
                    dataList.add(one);
                }
            } catch (SQLException ex) {
                getGameServer().getLogger().error("异常错误", ex);
            } finally {
                getGameServer().closeConnection(call, rs);
            }
        }
        return dataList;
    }

    //宴会帖有这个美女，却没有这个美女
    private List<Map<String,String>> checkInvitationBeauties(){
        List<Map<String,String>> beautyInvitationList = getBeautyInvitation();
        List<Map<String,String>> noBeautyInvitationList = getNoBeauty(beautyInvitationList);
        return noBeautyInvitationList;
    }

    //宴会有这个美女，却没有这个美女
    private List<Map<String,String>> checkBanquetBeauties(){
        List<Map<String,String>> beautyBanquetList = getBeautyBanquet();
        List<Map<String,String>> noBeautyBanquetList = getNoBeauty(beautyBanquetList);
        return noBeautyBanquetList;
    }

    private List<Map<String, String>> getBeautyInvitation() {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT bi.userId,bi.info FROM t_u_banquet_invitation bi" +
                " WHERE bi.type = 1";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                Map<String,String> info = JSON.parseObject(rs.getString("info"), new TypeReference<Map<String, String>>() {});
                temp.put("beautyId",info.get("beautyId"));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getNoBeauty(List<Map<String,String>> data) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT userId,beautyId FROM t_u_beauty WHERE userId = ? AND beautyId = ?";
        for(Map<String,String> one : data){
            try {
                call = getGameServer().getConnection().prepareCall(sql);
                call.setString(1, one.get("userId"));
                call.setString(2, one.get("beautyId"));
                rs = call.executeQuery();
                if(!rs.next()) {
                    dataList.add(one);
                }
            } catch (SQLException ex) {
                getGameServer().getLogger().error("异常错误", ex);
            } finally {
                getGameServer().closeConnection(call, rs);
            }
        }
        return dataList;
    }

    private List<Map<String, String>> getBeautyBanquet() {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT userId,invitationInfo FROM t_u_banquet" +
                " WHERE `type` = 20001";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                Map<String,String> invitationInfo = JSON.parseObject(rs.getString("invitationInfo"), new TypeReference<Map<String, String>>() {});
                temp.put("beautyId",invitationInfo.get("beautyId"));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }
}
